var www;
$(function() {
    var myclick = $('#my_submit');
    var tables = $('#teb_lsit');
    i=1
    myclick.click(function () {
        i++
        if(i>10){
            layer.msg('输入超限!', {icon: 6, time: 1000});
            return false
        }
        if(i==2){
          var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id1" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==3){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id2" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==4){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id3" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==5){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id4" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==6){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id5" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==7){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id6" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==8){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id7" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==9){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id8" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }else if (i==10){
            var a= i
            var add_list = $('<div class="row cl"><label class="form-label col-sm-2">折扣率'+i+'：</label><div class="formControls col-sm-9" ><input type="text" required class="input-text discount_rate_id" placeholder="请输入比率" name="id9" style="width:85px;">%</div></div>');
            add_list.appendTo(tables);
        }
    })
})
// 保存按钮
$("#form-member").validate({
    rules: {
        id:"idCheck",
        id1:"idCheck",
        id2:"idCheck",
        id3:"idCheck",
        id4:"idCheck",
        id5:"idCheck",
        id6:"idCheck",
        id7:"idCheck",
        id8:"idCheck",
        id9:"idCheck",
    },
    //提交表单数据
    submitHandler:function(form){
        if ($("#pinlei").find("option:selected").val()==0){
            if (www==1){
                layer.msg('没有品类存在', {icon: 6, time: 1000});
                return;
            }
            layer.msg('请选择品类', {icon: 6, time: 1000});
            return;
        }
        //提交
        layer.confirm('是否要生效您配置的品类折扣价',function(index){
            var numArr = [];
            var cesi =$("#discount_rate_id").val();
            $('.teb_lsit input').each(function(){
                numArr.push($(this).val());
            });
            pinlei=$("#pinlei").find("option:selected").val();
            console.log(numArr)
            console.log(cesi)
            var b=numArr.join(",")
            if(''==b){
                var arr=cesi
            }else{
                var arr=cesi+","+b
            }
            var quzhi=JSON.parse(web.readCookie("loginData"))
            var user_data = {
                //添加人
                CreationBy:quzhi.user.user_ID,
                //状态
                Status:0,
                //品类id
                CategoryId :pinlei,
                /*//更新人
                LastModifiedBy:quzhi.user.user_ID,*/
                //品类折扣率
                value:arr,
                //发布时间
                EffectiveDate:$("#logmin").val()
            }
            var iii=arr.split(",")
            var sss=iii.sort()
            for(var i=0;i<iii.length;i++) {
                if (parseFloat(sss[i]) == parseFloat(sss[i + 1])) {
                    return layer.msg('存在重复值!!!  '  +sss[i]+'已重复', {icon: 6, time: 3000});
                }
            }
            console.log(user_data)
            var succCallBack = function (data, status, response) {
                console.log(data);
                layer.msg('操作成功!', {icon: 6, time: 1000});
                parent.queryDataFn();
                setTimeout( function () {
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                },700);
            }
            //给一个接口地址提交数据
            //192.168.7.18:8080
            return web.doAjax(root.sroreinter.addDiscount, 'post', user_data,succCallBack);
        })
    }
})
//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
/*
下拉框的展示
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.discountcategory,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_level:1
        },
        //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bob").text());
                $("#pinlei").html(interText(dataInter));
                if (dataInter==""){
                    www=1
                }
            }
        }
    })
})
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}